import re
import requests
import lxml.html


etree = lxml.html.etree
class Spider:
    def start_request(self, url):
        response = requests.get(url)
        e_html = etree.HTML(response.text)
        html = response.text
        src_list = re.findall('src="(.*?)"', html)
        file_name_list = e_html.xpath('//div[@class="book-mid-info"]/h4/a/text()')
        for src, file_name in zip(src_list, file_name_list):
            img_url = "http:" + src
            print(img_url, file_name )
            self.down(img_url, file_name)

    def down(self, img_url, file_name):
        try:
            r = requests.get(img_url)
            with open('img2\\' + file_name + '.jpg', 'wb') as f:
                f.write(r.content)
                print("正在抓取: " + img_url)
        except:
            print("抓取错误")


spider = Spider()
url = "https://www.qidian.com/all"
spider.start_request(url)